/**
 * Copyright (C) 2009, 2010 SC 4ViewSoft SRL
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cc.mango.android.chartview;

import org.achartengine.chart.AbstractChart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;
import cc.mango.android.chart.BaseXYChart;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;

/**
 * The view that encapsulates the graphical chart.
 */
public class ChartView extends View
{
	/** The chart to be drawn. */
	private AbstractChart	mChart;
	/** The view bounds. */
	private Rect			mRect	= new Rect();

	/**
	 * Creates a new graphical view.
	 * 
	 * @param context the context
	 * @param chart the chart to be drawn
	 */
	public ChartView(Context context, AbstractChart chart)
	{
		super(context);
		mChart = chart;
	}

	/**
	 * Creates a new graphical view.
	 * 
	 * @param context the context
	 */
	public ChartView(Context context)
	{
		super(context);
	}

	public void setChart(AbstractChart chart)
	{
		mChart = chart;

	}

	@Override
	protected void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		canvas.getClipBounds(mRect);
		int top = mRect.top;
		int left = mRect.left;
		int width = mRect.width();
		int height = mRect.height();
		mChart.draw(canvas, left, top, width, height);

		TDStockLog.d("Print_Info", "mRect:" + mRect.toShortString());
	}

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{

		if (mChart instanceof BaseXYChart)
		{
			((BaseXYChart) mChart).handleTouch(event);
		}

		return true;
	}

}